<script setup lang="ts">
  import { useCreditLimitForm } from '@/services/pages/user/useUser'
  import { PropType, watch } from 'vue'
  import { HMessage, HMessageBox } from '@/components/common/message'
  import { amountForamt } from '@/utils/tools'

  const { form, formSaveLoading, formSave, resetForm } = useCreditLimitForm()

  defineOptions({
    name: 'UserCreditLimitModal',
  })

  const props = defineProps({
    userIds: {
      type: Array as PropType<Array<string>>,
      required: true,
    },
    creditLimit: {
      type: Number,
      default: () => 0,
    },
    settlementDeadLine: {
      type: Number,
      default: () => 0,
    },
  })

  const emits = defineEmits<{
    (e: 'update'): void
  }>()

  // 弹窗显示状态
  const visible = defineModel('modelValue', { type: Boolean, default: false })

  /**
   * 隐藏弹窗
   */
  const hideDialog = () => {
    resetForm()
    visible.value = false
  }

  /**
   * 结算期限输入事件
   */
  const onSettlementDeadlineInput = () => {
    const settlementDeadline = form.value.settlementDeadline.toString()
    form.value.settlementDeadline = settlementDeadline.replace(/[^\d]/g, '')

    if (Number(form.value.settlementDeadline) > 365) {
      form.value.settlementDeadline = '365'
    }
  }

  /**
   * 信用额度输入事件
   */
  const onCreditLimitInput = () => {
    form.value.creditLimit = amountForamt(form.value.creditLimit, 9)

    if (!form.value.creditLimit) {
      form.value.creditLimitType = 1
    }

    if (form.value.creditLimit === '0') {
      form.value.creditLimitType = 2
    }

    if (form.value.creditLimit > '0') {
      form.value.creditLimitType = 3
    }
  }

  /**
   * 确定事件
   */
  const onConfirm = async () => {
    const res = await formSave()
    if (res.code === 20000) {
      HMessage.success('保存成功')
      hideDialog()
      emits('update')
      return
    }

    HMessageBox.error(res.message)
  }

  watch(visible, (value) => {
    if (value) {
      if (props.userIds.length > 0) {
        form.value.userIds = props.userIds
      }

      if (props.creditLimit > 0) {
        form.value.creditLimit = props.creditLimit.toString()
      }

      if (props.settlementDeadLine > 0) {
        form.value.settlementDeadline = props.settlementDeadLine.toString()
      }

      onCreditLimitInput()
      onSettlementDeadlineInput()
    }
  })
</script>

<template>
  <h-dialog v-model="visible" title="修改信用额度" :loading="formSaveLoading" @close="hideDialog">
    <el-form :model="form" class="modal_form_content" inline-message label-position="top">
      <!--信用额度-->
      <el-form-item label="信用额度">
        <el-input
          v-model.trim="form.creditLimit"
          placeholder="请输入信用额度"
          @input="onCreditLimitInput"
        />
      </el-form-item>

      <!--结算期限-->
      <el-form-item label="结算期限（天）">
        <el-input
          v-model.trim="form.settlementDeadline"
          placeholder="请输入结算期限"
          clearable
          @input="onSettlementDeadlineInput"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <h-button type="primary" is-modal @click="onConfirm">确定</h-button>
      <h-button is-modal @click="hideDialog">取消</h-button>
    </template>
  </h-dialog>
</template>

<style scoped lang="scss">
  :deep(.el-input-number) {
    width: 100%;

    .el-input__inner {
      text-align: left;
    }
  }
</style>
